package com.cmsz.order.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;
import com.cmsz.framework.log.LogHandler;

/**
 * 
 * @author zhoujirong
 *
 */
@Controller
@RequestMapping("/third")
public class ThirdAction {

	private static LogHandler logger = LogHandler.getLogger(ThirdAction.class);
	
	@Value("${wxPayUrl}")
	private String wxPayUrl;
	@Value("${aliPayUrl}")
	private String aliPayUrl;
	@Value("${unionPayUrl}")
	private String unionPayUrl;
	@Value("${sinmulator}")
	private String sinmulator;
	
	/**
	 * 模拟第三方支付机构接收支付请求，只展示参数
	 * @param req
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/showParameters.action", produces = "text/html; charset=utf-8", method = { RequestMethod.POST,
			RequestMethod.GET })
	public @ResponseBody DeferredResult<String> showParameters(HttpServletRequest req) throws IOException {
		DeferredResult<String> deferredResult = new DeferredResult<String>();
		if (!"open".equals(sinmulator)) {
			deferredResult.setResult("非法请求，H5网站调试功能已关闭");
			return deferredResult;
		}
		Map<String, String> reqMap = comvertMap(req.getParameterMap());
		logger.info("虚拟三方机构|ThirdAction|接收到支付订单请求",null);
		logger.messageReceive("虚拟三方机构|ThirdAction|接收到支付订单请求:" + reqMap.toString());
		try {
			StringBuilder sb = new StringBuilder();
			sb.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body><form id=\"paysubmit\" name=\"paysubmit\" action=\""+aliPayUrl+"\" method=\"post\">");
			for(Map.Entry<String, String> entry : reqMap.entrySet()) {
				sb.append("<label>").append(entry.getKey()).append(":</label></br>");
				sb.append("<input type=\"text\" name=\""+entry.getKey()+"\" value=\""+entry.getValue()+"\" /></br>");
			}
			sb.append("</form></body></html>");
			deferredResult.setResult(sb.toString());
			logger.info("虚拟三方机构|ThirdAction|向客户端响应消息完成",null);
			logger.messageReceive("虚拟三方机构|ThirdAction|向客户端响应消息完成:" + sb.toString());
		} catch (Exception e) {
			logger.error("虚拟三方机构|ThirdAction|提交订单失败", e);
			deferredResult.setResult(e.getMessage());
		}
		return deferredResult;
	}
	
	/**
	 * 将http请求参数转换成Map<String, String>
	 * @param map
	 * @return
	 */
	private Map<String, String> comvertMap(Map<String, String[]> map) {
		Map<String, String> newMap = new HashMap<String, String>();
		Iterator<Entry<String, String[]>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Entry<String, String[]> entry = it.next();
			String key = entry.getKey();
			String[] value = entry.getValue();
			newMap.put(key, value[0]);
		}
		return newMap;
	}
}

